
#pragma strict

// The script that shoots the bullet.

var projectile : GameObject;
var fireSound : AudioClip;

private var speed :int = 100;
private var mass :int = 10;
private var drag :int = 0;

private var gameController : GameController;

private var spawnPositionProjectile : Vector3;
private var spawnRotationProjectile : Quaternion;

private var fireAudioSource : AudioSource;
private var audioVolume = 1;

private var shootTime : float = 2.0;
private var shootTimer : float = shootTime;
private var canShoot : boolean = true;

private var fireRate = 2.0;
 private var nextFire = 0.0;

function Awake(){
	fireAudioSource = gameObject.AddComponent(AudioSource);
	fireAudioSource.clip = fireSound;
	fireAudioSource.loop = false;
	fireAudioSource.volume = audioVolume;
	fireAudioSource.playOnAwake = false; //Abspielen wenn es initialisiert wird
}

function Start () {
	//Get the scripts game object to get the reference of the script
	gameController = GameObject.FindGameObjectWithTag("Scripts").GetComponent(GameController);
}

function Update () {

    if (Input.GetMouseButtonDown(0) && !gameController.isGUIEnabled()) {
    	Shoot(); // This will shoot if the time is ready.
    }
}

public function Shoot() {
	if(Time.time > nextFire) {
		nextFire = Time.time + fireRate;
    	
        spawnPositionProjectile = transform.position;
        // Fire in viewing direction.
        spawnRotationProjectile = transform.rotation;
        
        fireAudioSource.Play();
        
        // this.renderer.enabled = true;
        // Instantiate a bullet in front of the player.
        var go : GameObject = Instantiate(projectile, spawnPositionProjectile, spawnRotationProjectile);
        	go.rigidbody.mass = mass;
        	go.rigidbody.drag = drag;
        	go.rigidbody.transform.localScale = Vector3(0.4,0.4,0.4);
		
        	// The velocity is derived from the viewing direction.
        	go.rigidbody.velocity = transform.TransformDirection(Vector3(0, 0, speed));
    }
}